﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 自定义泛型集合1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Persons ps = new Persons();

            Person p = new Person() { Name = "Mali", Age = 13 };

            ps.Add(p);

            Men m = new Men() { Name = "Jim",Age = 14};
            //ps.Add(m);

            foreach (var item in ps)
            {
                Console.WriteLine((item as Person).Name);
            }

            Console.ReadKey();
        }
    }

    public class Men
    {
        protected int age;
        public int Age { get { return age; } set { age = value; } }
        protected string name;
        public string Name { get { return name; } set { name = value; } }
        public Men() { }
    }

    public class Person
    {
        protected int age;
        public int Age { get { return age; } set { age = value; } }
        protected string name;
        public string Name { get { return name; } set { name = value; } }

        public Person()
        {

        }
    }

    public class Persons : IList<Person>
    {
        private List<Person> list = new List<Person>();

        public Person this[int index] { get => list[index]; set => list[index] = value; }

        public int Count => list.Count;

        public bool IsReadOnly => false;

        public void Add(Person item)
        {
            list.Add(item);
        }

        public void Clear()
        {
            list.Clear();
        }

        public bool Contains(Person item) => list.Contains(item);

        public void CopyTo(Person[] array, int arrayIndex) => list.CopyTo(array, arrayIndex);

        public IEnumerator<Person> GetEnumerator() => list.GetEnumerator();


        public int IndexOf(Person item) => list.IndexOf(item);


        public void Insert(int index, Person item) => list.Insert(index, item);


        public bool Remove(Person item) => list.Remove(item);

        public void RemoveAt(int index) => list.RemoveAt(index);

        IEnumerator IEnumerable.GetEnumerator() => list.GetEnumerator();
    }
}
